package cn.usersmsg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;





import cn.usersmsg.model.User;
import cn.usersmsg.util.BaseDB;

public class UserDaoImpl implements IUserDao {
	Connection conn = null;
	PreparedStatement pst = null;
	ResultSet rs = null;
	String sql = null;
	/**
	 * 用户注册
	 * @return
	 */
	public int createUser(User user) {
		int n = -1;
		conn = BaseDB.getConn();
		sql = "insert into t_users values(null,?,?,?,0)";
		Object [] obj = {user.getUser_name(),user.getUser_pwd(),new java.sql.Date(user.getUser_date().getTime()).toLocaleString()};
		n = BaseDB.executeUpdate(sql, obj);
		BaseDB.close(conn, pst, rs);
		return n;
	}
	
	/**
	 * 根据用户名检查用户名是否被注册过
	 * @param name
	 * @return
	 */
	public int checkUser(String name) {
		int n = -1;
		conn = BaseDB.getConn();
		sql = "select user_id from t_users where user_name = ?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			rs = pst.executeQuery();
			if (rs.next()) {
				n = 1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			BaseDB.close(conn, pst, rs);
		}
		return n;
	}
	
	/**
	 * 根据用户名和密码验证用户登陆
	 * @param name
	 * @param pwd
	 * @return
	 */
	public User userLogin(String name,String pwd) {
		User user = null;
		conn = BaseDB.getConn();
		sql = "select * from t_users where user_name = ? and user_pwd = ?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			pst.setString(2, pwd);
			rs = pst.executeQuery();
			if (rs.next()) {
				user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),new java.util.Date(rs.getTimestamp(4).getTime()),rs.getInt(5));
			//如果成功登陆 则修改本次登陆时间
				sql = "update t_users set user_date = ? where user_name = ?";
				Object [] obj = {new java.sql.Date(new Date().getTime()).toLocaleString(),name};
				BaseDB.executeUpdate(sql, obj);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDB.close(conn, pst, rs);
		}
		return user;
	}
	
	/**
	 * 根据用户名和密码验证管理员登陆
	 * @param name
	 * @param pwd
	 * @return
	 */
	public User adminLogin(String name,String pwd) {
		User user = null;
		conn = BaseDB.getConn();
		sql = "select * from t_users where user_name = ? and user_pwd = ? and user_identity = 1";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			pst.setString(2, pwd);
			rs = pst.executeQuery();
			if (rs.next()) {
				user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),new java.util.Date(rs.getTimestamp(4).getTime()),rs.getInt(5));
			//如果成功登陆 则修改本次登陆时间
				sql = "update t_users set user_date = ? where user_name = ?";
				Object [] obj = {new java.sql.Date(new Date().getTime()).toLocaleString(),name};
				BaseDB.executeUpdate(sql, obj);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDB.close(conn, pst, rs);
		}
		return user;
	}
	
	/**
	 * 根据用户名获取用户信息
	 * @param name
	 * @return
	 */
	public User getUser(String name) {
		User user = null;
		conn = BaseDB.getConn();
		sql = "select * from t_users where user_name = ?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			rs = pst.executeQuery();
			if (rs.next()) {
				user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),new java.util.Date(rs.getTimestamp(4).getTime()),rs.getInt(5));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDB.close(conn, pst, rs);
		}
		return user;
	}
	
	/**
	 * 分页获取用户信息
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<User> getUsers(int pageNumber,int pageSize) {
		List<User> list = new ArrayList<User>();
		conn = BaseDB.getConn();
		sql = "select * from t_users where user_identity = 0 order by user_id limit ?,?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, (pageNumber-1)*pageSize);
			pst.setInt(2, pageSize);
			rs = pst.executeQuery();
			while(rs.next()){
				User user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),new java.util.Date(rs.getTimestamp(4).getTime()),rs.getInt(5));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDB.close(conn, pst, rs);
		}
		return list;
	}
	
	/**
	 * 分页获取搜索的用户信息
	 * @param pageNumber
	 * @param pageSize
	 * @param namelike
	 * @return
	 */
	public List<User> getUsers(int pageNumber,int pageSize,String namelike) {
		List<User> list = new ArrayList<User>();
		conn = BaseDB.getConn();
		sql = "select * from t_users where user_identity = 0 and user_name like ? order by user_id limit ?,?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, "%%"+namelike+"%%");
			pst.setInt(2, (pageNumber-1)*pageSize);
			pst.setInt(3, pageSize);
			rs = pst.executeQuery();
			while(rs.next()){
				User user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),new java.util.Date(rs.getTimestamp(4).getTime()),rs.getInt(5));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDB.close(conn, pst, rs);
		}
		return list;
	}
	
	/**
	 * 获取所有用户信息
	 * @return
	 */
	public List<User> allUser() {
		List<User> list = new ArrayList<User>();
		conn = BaseDB.getConn();
		sql = "select * from t_users where user_identity = 0 order by user_id";
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				User user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),new java.util.Date(rs.getTimestamp(4).getTime()),rs.getInt(5));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDB.close(conn, pst, rs);
		}
		return list;
	}
	
	/**
	 * 获取所有搜索用户信息
	 * @return
	 */
	public List<User> allUser(String namelike) {
		List<User> list = new ArrayList<User>();
		conn = BaseDB.getConn();
		sql = "select * from t_users where user_identity = 0 and user_name like ? order by user_id";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, "%%"+namelike+"%%");
			rs = pst.executeQuery();
			while(rs.next()){
				User user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),new java.util.Date(rs.getTimestamp(4).getTime()),rs.getInt(5));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			BaseDB.close(conn, pst, rs);
		}
		return list;
	}
	
	/**
	 * 删除用户
	 * @return
	 */
	public int delUser(int userid){
		int n = -1;
		conn = BaseDB.getConn();
		sql = "delete from t_users where user_id = ?";
		Object [] obj = {userid};
		n = BaseDB.executeUpdate(sql, obj);
		BaseDB.close(conn, pst, rs);
		return n;
	}
	
	/**
	 * 修改用户信息
	 * @param name
	 * @param pwd
	 * @param identyty
	 * @return
	 */
	public int updateUser(String name,String pwd,int identity,int id) {
		int n = -1;
		conn = BaseDB.getConn();
		sql = "update t_users set user_name = ?,user_pwd=?,user_identity=? where user_id=?";
		Object [] obj = {name,pwd,identity,id};
		n = BaseDB.executeUpdate(sql, obj);
		BaseDB.close(conn, pst, rs);
		return n;
	}

}
